<template>
	<view class="tcr-order">
		<scroll-view class="tcr-order-list" scroll-y="true" @scrolltolower="handleLower">
			<view v-for="item in orderList" :key="item.id" class="tcr-order-card">
				<view class="tcr-order-card-head">
					<view class="tcr-order-card-code">{{ item.orderCode }}</view>
					<view class="tcr-order-card-status">{{ handleOrderStatus(item.orderStatus) }}</view>
				</view>
				<view v-for="(goods, index) in item.detailList" :key="index" class="tcr-order-card-goods">
					<view class="tcr-order-card-goods-img">
						<image v-if="goods.goodsPic" :src="handleImagePath(goods.goodsPic)"></image>
						<image v-else src="../../static/imgs/gzh.jpg"></image>
					</view>
					<view class="tcr-order-card-goods-info">
						<view class="tcr-order-base">
							<view class="tcr-order-base-name tcr-ellipsis">{{ goods.goodsName }}</view>
							<view class="tcr-order-base-price">{{ goods.goodsPrice }}</view>
						</view>
						<view class="tcr-order-count">×{{ goods.buyCount }}</view>
					</view>
				</view>
				<view class="tcr-order-card-total">
					<text style="font-size: 28rpx; color: #666666;">总积分：</text>
					<text>{{ item.orderPrice }}</text>
				</view>
				<view class="tcr-order-card-btns">
					<button v-if="item.orderStatus == '1'" type="primary" size="mini" @click="useGoods(item)">使用</button>
					<!-- <button v-if="item.orderStatus == '1'" style="margin-left: 20rpx;" type="default" size="mini" @click="useGoods(item)">取消</button> -->
					<button v-if="item.orderStatus != '1'" style="margin-left: 20rpx;" type="default" size="mini" @click="delOrder(item)">删除</button>
				</view>
			</view>
		</scroll-view>
		<!-- 使用兑换 -->
		<uni-popup ref="useOrderDialog" type="dialog" :mask-click="false">
			<view class="use-goods-model">
				<image @click="handleCloseUse" class="use-goods-close" src="../../static/imgs/close.png"></image>
				<view class="use-goods-head">
					<text>使用兑换</text>
				</view>
				<view class="use-goods-body">
					<view class="use-goods-body-query">
						<input v-model="formData.sourceCode" class="uni-input" placeholder="请输入机器码" />
					</view>
				</view>
				<view class="use-goods-btns">
					<button @click="handleSubmitUse" class="mini-btn" type="primary" size="mini">确认</button>
					<button @click="handleCloseUse" class="mini-btn" type="default" size="mini">取消</button>
				</view>
			</view>
		</uni-popup>
		<!-- 激活码显示 -->
		<uni-popup ref="showSecretDialog" type="dialog" :mask-click="false">
			<view class="use-goods-model">
				<image @click="handleCloseSecret" class="use-goods-close" src="../../static/imgs/close.png"></image>
				<view class="use-goods-head">
					<text>激活码</text>
				</view>
				<view class="use-goods-body">
					<view class="use-goods-body-query tcr-auto-line">
						{{ secret }}
					</view>
				</view>
				<view class="use-goods-btns">
					<button @click="handleCopySecret" class="mini-btn" type="primary" size="mini">复制</button>
					<button @click="handleCloseSecret" class="mini-btn" type="default" size="mini">关闭</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import apis from '../../apis/apis.js';
	
	export default {
		data() {
			return {
				orderList: [],
				total: 0,
				queryParam: {
					pageNum: 1,
					pageSize: 5,
				},
				formData: {
					sourceCode: '',
					orderId: '',
					sourceName: '',
				},
				secret: '',
			};
		},
		onLoad() {
			this.queryOrder();
		},
		methods: {
			queryOrder() {
				this.$http.get({
					url: apis.getOrderList,
					data: this.queryParam,
				}).then(res => {
					if (res.code === 200) {
						this.orderList = [...this.orderList, ...res.rows];
						this.total = res.total;
					}
				}).catch(err => {
					console.log('调用接口失败：', err);
				})
			},
			handleLower() {
				// 触底触发
				if (this.total > this.orderList.length) {
					this.queryParam.pageNum = this.queryParam.pageNum + 1;
					this.queryOrder();
				}
			},
			useGoods(row) {
				// 使用
				this.formData.orderId = row.id;
				this.formData.sourceName = row.detailList[0].goodsName;
				this.$refs.useOrderDialog.open();
			},
			handleSubmitUse() {
				// 使用
				this.$http.post({
					url: apis.useOrder,
					data: this.formData,
				}).then(res => {
					if (res.code === 200) {
						this.secret = res.data.secret;
						this.$refs.showSecretDialog.open();
						this.handleCloseUse();
						// 重新加载订单信息
						this.orderList = [];
						this.queryParam.pageNum = 1;
						this.queryOrder();
					}
				}).catch(err => {
					console.log('调用接口失败：', err);
					if (err.data && err.data.msg) {
						uni.showToast({
							title: err.data.msg,
							icon: 'none',
							duration: 1500
						});
					}
				});
			},
			handleCloseUse() {
				this.formData.sourceCode = '';
				this.formData.sourceName = '';
				this.formData.orderId = '';
				this.$refs.useOrderDialog.close();
			},
			handleCopySecret() {
				const that = this;
				if (this.secret) {
					uni.setClipboardData({
						data: this.secret,
						success: function () {
							console.log('success');
							that.handleCloseSecret();
						}
					});
				}
			},
			handleCloseSecret() {
				this.secret = '';
				this.$refs.showSecretDialog.close();
			},
			delOrder(row) {
				// 删除订单
				this.$http.get({
					url: apis.deleteOrder + '/' + row.id,
				}).then(res => {
					if (res.code === 200) {
						uni.showToast({
							title: '删除成功',
							icon: 'none',
							duration: 1500
						});
						this.orderList = [];
						this.queryParam.pageNum = 1;
						this.queryOrder();
					}
				}).catch(err => {
					console.log('调用接口失败：', err);
				})
			},
			handleOrderStatus(stu) {
				if (stu == '0') {
					return '待付款';
				} else if (stu == '1') {
					return '待使用';
				} else if (stu == '2') {
					return '已使用';
				} else if (stu == '3') {
					return '已取消';
				} else {
					return '订单异常';
				}
			},
			handleImagePath(path) {
				return apis.imgUrl + '/' + apis.imgBucket +  '/' + path;
			},
		}
	}
</script>

<style lang="scss">
	.tcr-order {
		width: 100%;
		height: 100vh;
		box-sizing: border-box;
		overflow: hidden;
		
		&-list {
			height: 100vh;
		}
		
		&-card {
			margin: 20rpx;
			padding: 20rpx;
			border-radius: 20rpx;
			box-shadow: 0 2px 8px 0 rgba(0,0,0,0.2);
			
			&-head {
				display: flex;
				align-items: center;
				justify-content: space-between;
				
				.tcr-order-card-code {
					font-weight: 600;
					font-size: 32rpx;
					color: #d50003;
				}
				
				.tcr-order-card-status {
					font-size: 28rpx;
					color: #666666;
				}
			}
			
			&-goods {
				display: flex;
				margin-top: 10rpx;
				width: 100%;
				
				&-img {
					border-radius: 20rpx;
					width: 140rpx;
					
					> image {
						width: 140rpx;
						height: 140rpx;
					}
				}
				
				&-info {
					margin-left: 10rpx;
					flex: 1;
					width: 0;
					
					.tcr-order-base {
						display: flex;
						align-items: center;
						justify-content: space-between;
						width: 100%;
						margin-top: 10rpx;
						
						&-name {
							flex: 1;
						}
						
						&-price {
							width: 120rpx;
							text-align: right;
						}
					}
					
					.tcr-order-count {
						text-align: right;
						font-size: 28rpx;
						color: #999999;
						margin-top: 10rpx;
					}
				}
			}
			
			&-total {
				font-size: 32rpx;
				font-weight: 600;
				text-align: right;
				color: #333333;
			}
			
			&-btns {
				text-align: right;
				margin-top: 20rpx;
			}
		}
		
		.use-goods-model {
			background: #ffffff;
			width: 70vw;
			border-radius: 20rpx;
			padding: 20rpx;
			position: relative;
			
			> image {
				width: 200rpx;
				height: 200rpx;
				margin-bottom: 20rpx;
			}
			
			.use-goods-head {
				
			}
			
			.use-goods-body {
				padding: 20rpx 0;
				
				.use-goods-body-query {
					
					> input {
						border: 1px solid #f2f2f2;
						padding: 10rpx;
						border-radius: 10rpx;
					}
				}
			}
			
			.use-goods-btns {
				text-align: center;
				
				> button {
					margin-left: 20rpx;
				}
			}
			
			.use-goods-close {
				position: absolute;
				right: 20rpx;
				top: 20rpx;
				width: 40rpx;
				height: 40rpx;
			}
		}
	}

</style>
